﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SonCa.SocialNetwork.Web.ASCX.IPresenters;
using Core.DataAccess;
using SonCa.SocialNetwork.Web.ASCX.Presenters;
using System.ComponentModel.Composition;
using Core.Interfaces;
using Core.Common;

namespace SonCa.SocialNetwork.Web.ASCX
{
    public partial class CommentControl : System.Web.UI.UserControl, IComment
    {
        private CommentPresenter _presenter;
        [Import]
        private IUserSession _userSession;


        protected void Page_Load(object sender, EventArgs e)
        {
            MEFManager.Compose(this);
            _presenter = new CommentPresenter();
            _presenter.Init(this);
            _presenter.LoadComment();
            imgCommentAvatar.ImageUrl = "~/Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + _userSession.CurrentUser.AccountID.ToString();
            txtMyComment.Attributes.Add("class", this.SystemObjectRecordID.ToString());
            txtMyComment.Attributes.Add("style", "border: 0 solid #d9d9d9;");
            txtMyComment.Attributes.Add("placeholder", "Your Comment ........");
            txtMyComment.Attributes.Add("onkeypress", "return PostComment(event,this);return false;");
        }


        #region IComment

        public int SystemObjectID { get; set; }

        public long SystemObjectRecordID
        {
            get;
            set;
        }

        public void LoadComment(List<Comment> comments)
        {
            comments = comments.OrderBy(n => n.CreateDate.Date).ThenBy(n => n.CreateDate.TimeOfDay).ToList();
            if (comments.Count > 0)
            {
                pnlComment.Controls.Add(new LiteralControl("<div class='mgbot16'>"));
                foreach (var comment in comments)
                {
                    pnlComment.Controls.Add(new LiteralControl("<div class='mgbot15'><img src='" + "../Images/ProfileAvatar/ProfileImage.aspx?AccountID=" + comment.CommentByAccountID.ToString() + "'" + "width='35' height='35' class='img cpos_1' />"));
                    pnlComment.Controls.Add(new LiteralControl("<span class='title10'>" + comment.CommentByAccountName + "</span>" + "&nbsp;&nbsp;" + comment.Body + "<div>" + new SonCa.SocialNetwork.Web.HelperClass.RelativeTime().CalculateRelativeTime(comment.CreateDate) + "</div></div><div class='clearer'></div>"));
                }
                pnlComment.Controls.Add(new LiteralControl("</div>"));
            }
        }

        #endregion 
    }
}